package com.wl.dzt.sys.core.dao.select;

import com.wl.dzt.sys.core.dao.select.order.Sort;
import com.wl.dzt.sys.core.dao.select.term.Term;

import java.util.*;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 查询对象
 * 封装了sql语句的查询条件分组排序等
 */
public class Select {
    /**
     * 条件
     */
    private List<Term> wheres = new ArrayList<>();

    /**
     * 排序
     */
    private List<Sort> orders = new ArrayList<>();

    /**
     * 查询类
     */
    private Class<?> clazz;

    /**
     * 查询字段
     */
    private Set<String> fields = new HashSet<String>();

    /**
     * 第几条开始,取几条
     */
    private Integer start = 0, limit = 0;

    /**
     * sql表达式
     */
    private String sqlExp;

    private Select(Class<?> clazz) {
        this.clazz = clazz;
        this.fields.add("id");
    }

    private Select(String sqlExp) {
        this.sqlExp = sqlExp;
    }

    /**
     * <b>创建查询对象 </b>
     * <p>
     * 根据表达式来创建查询对象
     * </p>
     *
     * @param sqlExp
     * @return
     */

    public static Select create(String sqlExp) {
        return new Select(sqlExp);
    }

    /**
     * <b>创建查询对象 </b>
     * <p>
     * 根据实体类创建查询对象
     * </p>
     *
     * @param clazz
     * @return
     */

    public static Select create(Class<?> clazz) {
        return new Select(clazz);
    }

    /**
     * <b>分页</b>
     * <p>
     * 设置分页
     * </p>
     *
     * @param start
     * @param limit
     * @return
     */
    public Select page(Integer start, Integer limit) {
        this.start = start;
        this.limit = limit;
        return this;
    }

    /**
     * <b>添加条件</b>
     * <p>
     * 添加查询条件
     * </p>
     *
     * @param terms
     * @return
     */
    public Select where(Term... terms) {
        wheres.addAll(Arrays.asList(terms));
        return this;
    }

    /**
     * <b>添加排序</b>
     * <p>
     * 添加排序
     * </p>
     *
     * @param sorts
     * @return
     */
    public Select order(Sort... sorts) {
        this.orders.addAll(Arrays.asList(sorts));
        return this;
    }

    /**
     * <b>添加字段 </b>
     * <p>
     * 设置要查询的字段
     * </p>
     *
     * @param fields
     * @return
     */
    public Select fields(String... fields) {
        this.fields.addAll(Arrays.asList(fields));
        return this;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public List<Term> getWheres() {
        return wheres;
    }

    public List<Sort> getOrders() {
        return orders;
    }

    public Set<String> getFields() {
        return fields;
    }

    public Integer getStart() {
        return start;
    }

    public Integer getLimit() {
        return limit;
    }

    public String getSqlExp() {
        return sqlExp;
    }

    public void setSqlExp(String sqlExp) {
        this.sqlExp = sqlExp;
    }

}
